using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using IGlobalist.Cms.Common;
using System.IO;
using IGlobalist.Cms.Model;
using IGlobalist.Cms.Bll;
using System.Collections.Generic;

namespace IGlobalist.Cms.Admin.Control
{
    public partial class ResourceFileAddEdit : AdminBaseUserControl
    {
        public event EventHandler SaveContent;
        public event EventHandler EventAddEditFile;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                IList<IResourceFolder> resourceFolderList = ResourceFolderController.GetResourceFolderTreeForBind(0);
                resourceFolderList.RemoveAt(0);
                
                ddlFolderList.DataTextField = "Name";
                ddlFolderList.DataValueField = "ID";
                ddlFolderList.DataSource = resourceFolderList;
                ddlFolderList.DataBind();                
                IList<ICmsClass> classList = ClassController.GetClassListByClassTypeID((short)CmsObjectType.ResourceFile);
                
                ddlClassFileList.DataTextField = "Name";
                ddlClassFileList.DataValueField = "ID";
                ddlClassFileList.DataSource = classList;
                ddlClassFileList.DataBind();
            }
        }
        
        public void DoShow(object sender, EventArgs e)        
        {
            Visible = true;
        }
        public string SelectedID
        {
            get { return (string)ViewState["_SelectedFolder"]; }
            set { ViewState["_SelectedFolder"] = value; }
        }
        public void DoBindDropDownListFolder(object sender, EventArgs e)
        {
            ResourceFolderTree folderTree = (ResourceFolderTree)sender;
            SelectedID = folderTree.SelectedValue;
        }

        public void DoBindFile(object sender, EventArgs e)
        {
            ResourceFileList rfl = (ResourceFileList)sender;
            IResourceFile resourceFile = ResourceFileController.GetResourceFile(Convert.ToInt64(rfl.SelectedValue));
            ddlFolderList.SelectedValue = Convert.ToString(resourceFile.FolderID);
            lblStatus.Text = "Edit file";
            ICmsClass clazz = ClassController.GetClass(Convert.ToInt64(ddlClassFileList.SelectedValue));
            ucCmsExtPropertiesForm.PropertyList = clazz.PropertyList;
            if (ucCmsExtPropertiesForm.PropertyList.Count > 7) fsExtend.Visible = true;
            else fsExtend.Visible = false;   
            //ddlImageType.SelectedValue = resourceFile.Type.ToString();
            //ddlFolderList.SelectedValue = 
        }
        public void DoHidden(object sender, EventArgs e)
        {
            Visible = false;
        }
        public void DoAddFile(object sender, EventArgs e)
        {
            ViewState["_resourceFildID"] = null;
            lblStatus.Text = "Upload File";
            ICmsClass clazz = ClassController.GetClass(Convert.ToInt64(ddlClassFileList.SelectedValue));                lblError.Text = "";
            ucCmsExtPropertiesForm.PropertyList = clazz.PropertyList;
            if (ucCmsExtPropertiesForm.PropertyList.Count > 7) fsExtend.Visible = true;
            else fsExtend.Visible = false;   

            foreach (ListItem itemchidl in ddlFolderList.Items)
            {
                itemchidl.Selected = false;
            }
            ListItem item = ddlFolderList.Items.FindByValue(SelectedID);
            if (item != null)
            {
                item.Selected = true;
            }
            
        }
        public void DoSaveFile(object sender, EventArgs e)
        {
            if (fuFile.HasFile && ddlImageType.SelectedIndex != 0)
            {
                string path = Path.Combine(DataRootPhysicalPath, "FileUpload");
                HttpPostedFile fPost = fuFile.PostedFile;
                string fName = fuFile.FileName;
                string fExtention = Path.GetExtension(fPost.FileName);
                long fSize = Convert.ToInt64(fPost.ContentLength);
                IResourceFile resourceFile = new ResourceFile();
                Session[SessionVariable.Folder.CurrenttFolderID] = ddlFolderList.SelectedValue;
                resourceFile.ExtPropertyList = ucCmsExtPropertiesForm.PropertyList;
                resourceFile.Class = ClassController.GetClass(Convert.ToInt64(ddlClassFileList.SelectedValue));
                resourceFile.ClassID = Convert.ToInt64(ddlClassFileList.SelectedValue);
                resourceFile.FolderID = Convert.ToInt64(ddlFolderList.SelectedValue);
                resourceFile.ClassTypeID = (short)CmsObjectType.ResourceFile;
                resourceFile.Extention = fExtention;
                resourceFile.Type = (ResourceType)Convert.ToInt32(ddlImageType.SelectedValue);
                resourceFile.Name = fName;
                resourceFile.Size = fSize;
                resourceFile.UploadDate = DateTime.Now;
                resourceFile.EditedDate = DateTime.Now;
                resourceFile.Author = IGlobalistMembership.GetUser().ID;
                long numLongSave = ResourceFileController.InsertResourceFile(resourceFile);
                fPost.SaveAs(Path.Combine(path, numLongSave + fExtention));
                lblStatus.Text = "Insert File Successful";
                if (SaveContent != null)
                {
                    SaveContent(this, e);
                }
            }
            else
            {
                lblError.Text = "Require upload file";
            }


        }

        public void DoDeleteFile(object sender, EventArgs e)
        {

        }
        public void DoCopyFile(object sender, EventArgs e)
        {

        }

        protected void btnUpload_Click(object sender, EventArgs e)
        {                
            
            
        }

        protected void ddlClassFileList_SelectedIndexChanged(object sender, EventArgs e)
        {         
            Session[SessionVariable.Clazz.CurrentID] = ddlClassFileList.SelectedValue;
            ICmsClass clazz = ClassController.GetClass(Convert.ToInt64(ddlClassFileList.SelectedValue));
            ucCmsExtPropertiesForm.PropertyList = clazz.PropertyList;
            if (ucCmsExtPropertiesForm.PropertyList.Count > 7) fsExtend.Visible = true;
            else fsExtend.Visible = false;         
        }
    }
}